﻿using FreeRedis;

namespace CTP.CRM.Core.Redis
{
    public static class RedisStartupExtensions
    {
        public static IServiceCollection AddRedis(this IServiceCollection services, bool showLogInfo = false)
        {
            IConfiguration configuration = services.GetConfiguration();
            ConnectionStringBuilder options = configuration.GetSection("Redis").Get<ConnectionStringBuilder>();
            if (options == null)
            {
                throw new Exception("未找到Redis相关配置，请检查配置文件中是否包含名为【Redis】的节点");
            }

            RedisHelper.Init(options, showLogInfo);
            services.AddSingleton(RedisHelper.Client);
            services.AddSingleton((IServiceProvider s) => new RedisFacotry(options, s.GetRequiredService<ILoggerFactory>()));
            return services;
        }
    }
}
